package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.model.Pagination;
import com.cloudbroker.bcs.common.model.SortMarker;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.chat.ChatBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.chat.back.ChatMessageHistoryResult;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.chat.back.ChatMessageHistoryResultData;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.chat.vo.ChatMessageHistoryQueryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.common.cache.CacheBizService;
import com.cloudbroker.bcs.platform.ltb.middle.entity.ChatMsg;
import com.cloudbroker.bcs.platform.ltb.middle.service.ChatMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by world on 17/1/17.
 */
@Service
public class ChatBizServiceImpl implements ChatBizService {
    @Resource
    private ChatMsgService chatMsgService;

    public ChatMessageHistoryResult getChatHistory(ChatMessageHistoryQueryForm form) {
        ChatMsg chatMsg = new ChatMsg();
        chatMsg.setCompany_id(form.getCompany_id());
        chatMsg.setFrom_user_id(form.getUser().getUser_id());
        chatMsg.setTarget_id(form.getTarget_id());
        Pagination page = new Pagination();
        page.setPage_size(form.getRequest_num());
        page.setPage_no(form.getPage_no());
        chatMsg.setPage(page);
        List<SortMarker> sortMarkers = new ArrayList<>();
        SortMarker sortMarker = new SortMarker("dateTime",false);
        sortMarkers.add(sortMarker);
        chatMsg.setSortMarkers(sortMarkers);
        List<ChatMsg> chatMsgs = chatMsgService.query(chatMsg);

        ChatMessageHistoryResult chatMessageHistoryResult = new ChatMessageHistoryResult();
        List<ChatMessageHistoryResultData> chatMessageHistoryResultDatas = new ArrayList<>();
        for (ChatMsg temp : chatMsgs) {
            ChatMessageHistoryResultData data = new ChatMessageHistoryResultData();
            data.setSource(temp.getSource());
            data.setMsgUID(temp.getMsgUID());
            data.setClassname(temp.getClassname());
            data.setContent(temp.getContent());
            data.setDateTime(temp.getDateTime());
            data.setTargetId(temp.getTarget_id());
            data.setFromUserId(temp.getFrom_user_id());
            data.setGroupId(temp.getGroupId());
            data.setExtra(temp.getExtra());
            chatMessageHistoryResultDatas.add(data);
        }
        chatMessageHistoryResult.setData_list(chatMessageHistoryResultDatas);
        chatMessageHistoryResult.setTotal_item_num(chatMsgService.count(chatMsg));
        return VOUtil.setSuccessResult(chatMessageHistoryResult);
    }
}
